package com.example.ui.leftmenu;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.learn.util.CurriculumCenter;
import com.example.ui.learn.util.MyListViewAdapter1;
import com.example.ui.share.CircleHomeActivity;
import com.example.ui.share.CreateCircleGroupActivity;
import com.example.ui.share.adapter.AroundCircleAdapter;
import com.example.util.Util;

@SuppressLint("HandlerLeak")
public class Item06View extends BaseView {
	private Context context;
	private AroundCircleAdapter item_adapter;
	private ArrayList<Circle> item6_c = new ArrayList<Circle>();
	private Button titleLeftBtn, titleRightBtn;
	private TextView titleText;
	public static ListView listview;
	private String data;
	private RadioGroup i6_rg;
	String result;
	HSApplication hsApplication;

	public Item06View(Context context, HSApplication hsApplication) {
		this.context = context;
		this.hsApplication=hsApplication;
	}

	public void init() {
		view = LayoutInflater.from(context).inflate(R.layout.item06, null);
		titleLeftBtn = (Button) view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn = (Button) view.findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) view.findViewById(R.id.layout_title_middle_text);
		listview = (ListView) view.findViewById(R.id.i6_lv);
		titleLeftBtn.setText("菜单");
		titleText.setText("学习圈");
		titleRightBtn.setText("创建");

		i6_rg = (RadioGroup) view.findViewById(R.id.circle_tab_radio);

		item_adapter = new AroundCircleAdapter(context, item6_c);

		listview.setAdapter(item_adapter);
		titleLeftBtn.setOnClickListener(i6_ocl);
		titleRightBtn.setOnClickListener(i6_ocl);
		i6_rg.setOnCheckedChangeListener(radioGroupClick);
		new Thread(item6_nearby).start();
	}

	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 2:
				item_adapter.setCcInfos(item6_c);
				item_adapter.notifyDataSetChanged();

				listview.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View v,
							int position, long id) {
						Intent intent = new Intent(context,
								CircleHomeActivity.class);
						intent.putExtra("headImg", item6_c.get(position)
								.getThumb());
						intent.putExtra("titleName", item6_c.get(position)
								.getTitle());
						intent.putExtra("intro", item6_c.get(position)
								.getIntro());
						intent.putExtra("circleMemberNum", item6_c
								.get(position).getJoinNum());
						intent.putExtra("circleShareNum", item6_c.get(position)
								.getShareNum());
						intent.putExtra("circleId", item6_c.get(position)
								.getId());
						intent.putExtra("createCircleUserId", item6_c.get(position)
								.getUid());
						intent.putExtra("typeId", item6_c.get(position)
								.getTypeId());
						context.startActivity(intent);
					}
				});
				break;
			case 1:
				Toast.makeText(context, "未找到数据", Toast.LENGTH_SHORT).show();
				item_adapter.notifyDataSetChanged();
				break;

			default:
				break;
			}

		};

	};
	// 附近圈
	private Runnable item6_nearby = new Runnable() {

		@Override
		public void run() {
			data = NetRequest.postRequestCircleAll();

			JSONObject jo;
			try {
				jo = new JSONObject(data);
				result = jo.getString("response");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (result.equals("true")) {
				item6_c = AnalyzingJson.getdata_Circle(data);
				h.sendEmptyMessage(2);

			} else {
				h.sendEmptyMessage(1);
			}

		}
	};
	// 我的圈
	private Runnable item6_my = new Runnable() {

		@Override
		public void run() {

			data = NetRequest.postRequestCircleMy();
			JSONObject jo;
			try {
				jo = new JSONObject(data);
				result = jo.getString("response");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (result.equals("true")) {
				item6_c = AnalyzingJson.getdata_Circle(data);
				h.sendEmptyMessage(2);
			} else {
				h.sendEmptyMessage(1);
			}

		}
	};
	// 热门圈
	private Runnable item6_hot = new Runnable() {

		@Override
		public void run() {

			data = NetRequest.postRequestCircleHot();
			JSONObject jo;
			try {
				jo = new JSONObject(data);
				result = jo.getString("response");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (result.equals("true")) {
				item6_c = AnalyzingJson.getdata_Circle(data);
				h.sendEmptyMessage(2);
			} else {
				h.sendEmptyMessage(1);
			}

		}
	};

	private OnClickListener i6_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.layout_title_left_btn:
				myMoveView.showHideLeftMenu();
				if (MainService.isLeftMenu == true) {
					listview.setEnabled(false);
					i6_rg.setEnabled(false);
					i6_rg.setOnCheckedChangeListener(null);
				} else {
					listview.setEnabled(true);
					i6_rg.setEnabled(true);
					i6_rg.setOnCheckedChangeListener(radioGroupClick);
				}
				break;
			case R.id.layout_title_right_btn:
				Intent i1_l = new Intent();
				i1_l.setClass(context, CreateCircleGroupActivity.class);
				i1_l.putExtra("create_update", "create");
				context.startActivity(i1_l);
				break;
			default:
				break;
			}
		}
	};

	private OnCheckedChangeListener radioGroupClick = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_button0:// 首页
				new Thread(item6_nearby).start();
				titleText.setText("附近圈");
				titleRightBtn.setVisibility(View.VISIBLE);
				titleRightBtn.setText("创建");
				break;
			case R.id.radio_button1:// 信息
				new Thread(item6_my).start();

				titleText.setText("我的圈");
				titleRightBtn.setVisibility(View.INVISIBLE);
				break;
			case R.id.radio_button2:// 资料

				new Thread(item6_hot).start();
				titleText.setText("热门圈");
				titleRightBtn.setVisibility(View.INVISIBLE);
				break;
			default:
				break;
			}
		}
	};

}
